package com.xtaller.gateway.api;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.core.base.ViewApi;
import com.xtaller.core.format.J;
import com.xtaller.core.format.M;
import com.xtaller.core.format.R;
import com.xtaller.gateway.model.User;


import com.xtaller.gateway.repository.query.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Taller on 2017/7/27.
 */
@RestController
@RequestMapping("/admin/menu")
public class MenuApi extends ViewApi {
    @Autowired
    private com.xtaller.gateway.repository.opt.UserRepository userOptRepository;
    @Autowired
    private UserRepository userQueryRepository;


    @GetMapping("/v1")
    public Object getMenu(){
        logger.debug("addUser","1");
        JSONObject object = new JSONObject();
        JSONObject user = new JSONObject();
        List<JSONObject> info = new ArrayList<JSONObject>();

        user.put("loginName","taller");
        user.put("password","123456");


        JSONObject info1 = new JSONObject();
        JSONObject info2 = new JSONObject();
        JSONObject info3 = new JSONObject();
        info1.put("attr","age");
        info1.put("val",30);
        info2.put("val","airshy@163.com");
        info2.put("attr","mail");
        info3.put("val","122158003");
        info3.put("attr","qq");

        info.add(info1);
        info.add(info2);
        info.add(info3);

        object.put("user",user);
        object.put("info",info);
        System.out.println(J.o2s(object));
        User u = new User();
        u.setLoginName("AAAA");
        u.setPassword("ccccc");
        User u2 = new User();
        u2.setLoginName("AAAA2");
        u2.setPassword("ccccc2");
        //.save(u2);
//        if(optRepository.save(u) != null){
//            System.out.println("success");
//        }else {
//            System.out.println("error");
//        }

        List<JSONObject> menus = new ArrayList<JSONObject>();
        JSONObject base = new JSONObject();
        base.put("title","系统管理");
        base.put("icon","fa-cubes");
        base.put("spread",1==1?true:false);

        List<JSONObject> baseSub = new ArrayList<JSONObject>();
        JSONObject baseSub1 = new JSONObject();
        baseSub1.put("title","用户管理");
        baseSub1.put("icon","fa-table");
        baseSub1.put("href","/table");

        JSONObject baseSub2 = new JSONObject();
        baseSub2.put("title","用户组管理");
        baseSub2.put("icon","fa-check-square-o");
        baseSub2.put("href","/table1");

        JSONObject baseSub3 = new JSONObject();
        baseSub3.put("title","角色管理");
        baseSub3.put("icon","fa-github");
        baseSub3.put("href","/table2");
        baseSub.add(baseSub1);
        baseSub.add(baseSub2);
        baseSub.add(baseSub3);
        base.put("children",baseSub);


        menus.add(base);
        return menus;
    }

    @PostMapping("/v1")
    public Object addMenu(@RequestBody String objects, @RequestHeader Map<String,String> header){
        JSONObject object = new JSONObject();
        JSONObject user = new JSONObject();
        List<JSONObject> info = new ArrayList<JSONObject>();

        user.put("loginName","taller");
        user.put("password","123456");


        JSONObject info1 = new JSONObject();
        JSONObject info2 = new JSONObject();
        JSONObject info3 = new JSONObject();
        info1.put("attr","age");
        info1.put("val",30);
        info2.put("val","airshy@163.com");
        info2.put("attr","mail");
        info3.put("val","122158003");
        info3.put("attr","qq");

        info.add(info1);
        info.add(info2);
        info.add(info3);

        object.put("user",user);
        object.put("info",info);
        System.out.println(J.o2s(object));
//        if(userService.addUser(object)){
//            System.out.println("success");
//        }else {
//            System.out.println("error");
//        }
        return R.data(M.data(object));
    }

    @GetMapping("/test/menu")
    public Object getTestMenu(){
        List<JSONObject> menu = new ArrayList<>();
        JSONObject m1 = new JSONObject();
        m1.put("icon","&#xe8d3;");
        m1.put("title","用户管理");
        m1.put("url","/view/demo.html");
        JSONObject m2 = new JSONObject();
        m2.put("icon","&#xe8c1;");
        m2.put("title","Token管理");
        m2.put("url","/view/demo2.html");
        menu.add(m1);
        menu.add(m2);
        return R.data(M.data(menu).put("total",21));
    }

    @GetMapping("/{id}")
    public Object gets(@PathVariable("id") String id){
        User u = new User();
        u.setPassword("11111");
        u.setLoginName("aaaaa");
        u.setSalt(31121);
        if(id.equals("1")){
            u.setCreateTime(90001);
            userOptRepository.save(u);
        }else{
            userQueryRepository.save(u);
        }

        return R.data(M.data(u));
    }
}
